<?php
namespace app\api\controller;

use think\Request;

/**
 * @SWG\Swagger(
 *     schemes={"https","http"},
 *     host="bike.wei-yong.com/api/Meetings",
 *     consumes={"multipart/form-data"},
 *     produces={"application/json"},
 *     @SWG\Info(
 *       version="1.0",
 *       title="会议行程帮",
 *       description="会议行程帮接口文档<br> 以后就在这里愉快的对接口把!<br>"
 *     ),
 *     
 *     @SWG\Tag(
 *       name="Public",
 *       description="公共",
 *     ),
 *     
 *     @SWG\Tag(
 *       name="Guest",
 *       description="嘉宾端",
 *     ),
 *     
 *     @SWG\Tag(
 *       name="Driver",
 *       description="司机端",
 *     ),
 *     
 *     @SWG\Tag(
 *       name="Staff",
 *       description="员工端",
 *     ),
 * )
 */
class Meetings extends Base
{
    /**
     * @SWG\Post(
     *   path="/getHotel",
     *   tags={"Public"},
     *   summary="获取所有酒店",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据",
     *       @SWG\Header(header="id", type="string", description="酒店ID"),
     *       @SWG\Header(header="name", type="string", description="酒店名称"),
     *   )
     * )
     */
    public function getHotel()
    {
        $res = db('huiyi_hotel')->field('id, name')->limit(1000)->select();
        
        suc('success', $res);
    }

    /**
     * @SWG\Post(
     *   path="/getMeetingDetial",
     *   tags={"Guest"},
     *   summary="获取会议详情 - 首页",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据",
     *       @SWG\Header(header="img", type="string", description="会议大图"),
     *       @SWG\Header(header="title", type="string", description="会议名称"),
     *       @SWG\Header(header="going_time", type="string", description="会议具体进行时间"),
     *       @SWG\Header(header="address", type="string", description="会议地址"),
     *       @SWG\Header(header="mobile", type="string", description="会议电话"),
     *       @SWG\Header(header="company", type="string", description="会议举办方"),
     *       @SWG\Header(header="about", type="string", description="会议简介"),
     *       @SWG\Header(header="content", type="string", description="会议内容"),
     *       @SWG\Header(header="is_plan", type="integer", description="是否创建行程 - 0否，1是"),
     *   )
     * )
     */
    public function getMeetingDetial()
    {
        
        // 查询会议
        $resMeeting = db('huiyi_meeting')->where(['status'=>1])->find();
        
        if ( ! $resMeeting) {
            err('暂无会议');
        }
        
        $resMeeting['grade'] = json_decode($resMeeting['grade'], true);
        
        $where = [];
        $where['create_time'] = date("Y-m-d");
        $where['meeting_id'] = $resMeeting['id'];
        //访问详情自动增加会议浏览量
        $meeting_log = db('huiyi_meeting_log')->where($where)->field('id,create_time,meeting_id')->find();
        if ($meeting_log) {
            db('huiyi_meeting_log')->where(['id'=>$meeting_log['id']])->setInc('browse');
        }else{
            $data = [];
            $data['admin_id'] = $this->accessData['aid'];
            $data['browse'] = 1;
            $data['meeting_id'] = $resMeeting['id'];
            $data['create_time'] = date("Y-m-d");
            db('huiyi_meeting_log')->insert($data);
        }
       
        // 查询小程序用户是否绑定嘉宾并且填写 过行程规划
        $where = ['g.user_id'=>$this->accessData['uid'], 'g.meeting_id'=>$resMeeting['id']];
        
        $resGuest = db('huiyi_guest')->alias('g')
            ->join('huiyi_journey j', 'g.id = j.guest_id')
            ->where($where)
            ->count();
        
        $is_plan = 0;
        
        if ($resGuest > 0) {
            $is_plan = 1;
        }
        
        $returnData = [];
        $returnData['meeting'] = $resMeeting;
        $returnData['is_plan'] = $is_plan;
        
        suc('查询成功', $returnData);
    }

    
    
    /**
     * @SWG\Post(
     *   path="/addJourney",
     *   tags={"Guest"},
     *   summary="增加行程规划",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="name", description="姓名", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="gender", description="性别（男：1，女：2）", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="mobile", description="手机号码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="id_card", description="身份证", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="position", description="职务", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="city", description="城市", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="company", description="单位", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="job_title", description="职称", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="travel_contact", description="行程联系人姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="travel_mobile", description="行程联系人电话", in="formData", required=false, type="string"),
     *   
     *   @SWG\Parameter(name="vehicle", description="交通工具（飞机，高铁，自理，本地）", in="formData", required=false, type="string"),
     *   
     *   @SWG\Parameter(name="go_date", description="来程日期", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_number", description="来程航班", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_start_addr", description="来程航段 - 开始", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_end_addr", description="来程航段 - 结束", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_start_time", description="来程起飞时间", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_end_time", description="来程降落时间", in="formData", required=false, type="string"),
     *   
     *   @SWG\Parameter(name="return_date", description="返程日期", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_number", description="返程航班", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_start_addr", description="返程航段  - 开始", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_end_addr", description="返程航段  - 结束", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_start_time", description="返程起飞时间", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_end_time", description="返程降落时间", in="formData", required=false, type="string"),
     *   
     *   @SWG\Parameter(name="comment", description="注意事项", in="formData", required=false, type="string"),
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据",
     *   )
     * )
     */
    public function addJourney()
    {
        $postGuest = request()->only(['meeting_id', 'name', 'gender', 'id_card', 'mobile', 'position', 'city', 'company', 'job_title', 'travel_contact', 'travel_mobile'], 'post');
        
        $postJourney = request()->only(['meeting_id', 'vehicle', 'go_date', 'go_number', 'go_start_addr','go_end_addr', 'go_start_time', 'go_start_time', 'go_end_time', 'return_date', 'return_number', 'return_start_addr', 'return_end_addr', 'return_start_time', 'return_end_time', 'comment'], 'post');
        
        // 查询嘉宾信息是否创建
        $whereGuest = ['user_id'=>$this->accessData['uid'], 'meeting_id'=>$postGuest['meeting_id']];
        $resGuest = db('huiyi_guest')->where($whereGuest)->find();
        
        if ( ! $resGuest) {
            // 增加嘉宾信息
            $postGuest['create_time'] = $this->timeStamp;
            $postGuest['user_id'] = $this->accessData['uid'];
            $insertId = db('huiyi_guest')->insertGetId($postGuest);
            
            // 增加行程信息
            $postJourney['guest_id'] = $insertId;
            // 创建二维码
            $postJourney['qrcode'] = $this->createRqcode("meeting_id=" . $postGuest['meeting_id'] . "&guest_id=$insertId");  
            
            db('huiyi_journey')->insert($postJourney);
        }
        
        suc('', ['guest_id'=>$postJourney['guest_id']]);
    }

    /**
     * @SWG\Post(
     *   path="/getJourney",
     *   tags={"Guest"},
     *   summary="获取会议、嘉宾、行程、酒店、用车等信息",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="lat", description="经度", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="long", description="纬度", in="formData", required=true, type="string"),
     *   
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="meeting", type="string", description="会议信息"),
     *       @SWG\Header(header="guest", type="string", description="嘉宾信息"),
     *       @SWG\Header(header="journey", type="string", description="嘉宾行程信息"),
     *       @SWG\Header(header="hotel", type="string", description="酒店信息"),
     *       @SWG\Header(header="car", type="string", description="用车信息"),
     *   )
     * )
     */
    public function getJourney()
    {
        $post = request()->only(['meeting_id','guest_id', 'lat', 'long'], 'post');
        
        $return = [];
        $return['meeting'] = [];
        $return['guest'] = [];
        $return['journey'] = [];
        $return['hotel'] = [];
        $return['car'] = [];
        $return['status'] = [];
        
        //获取来程出票状态
        $my_journey = db('huiyi_journey')->where(['id'=>$post['meeting_id'],'guest_id'=>$post['guest_id']])->find();
        
        #来程状态
        if ($my_journey['vehicle'] != 3){
            if (!empty($my_journey['go_money'])){
                $return['status']['go_journey'] = 1;//自行出表
            }else{
                $return['status']['go_journey'] = 2;//行程确认中
            }
        }else{
            $return['status']['go_journey'] = 3;//自理
        }
        
        #返程状态
        if ($my_journey['vehicle'] != 3){
            if (!empty($my_journey['return_money'])){
                $return['status']['return_journey'] = 1;//自行出表
            }else{
                $return['status']['return_journey'] = 2;//行程确认中
            }
        }else{
            $return['status']['return_journey'] = 3;//自理
        }
        
        #酒店状态
        if ($my_journey['hotel_id'] == 0){
            $return['status']['my_hotel'] = 2;//酒店自付
        }else{
            $hotel = db('huiyi_hotel_log')->where(['id'=>$my_journey['hotel_id']])->find();
            $return['status']['my_hotel'] = $hotel['payment_type'];
        }
        
        #车辆状态
        if ($my_journey['car_id'] == 0){
            $return['status']['my_car'] = 2;//自行安排
        }else{
            $return['status']['my_car'] = 1;//大会安排
        }
        
        // 会议信息
        $resMeeting = db('huiyi_meeting')->where(['id'=>$post['meeting_id']])->find();
        $return['meeting'] = $resMeeting;
        
        // 获取嘉宾信息
        $where = [];
        $where = ['user_id'=>$this->accessData['uid'], 'meeting_id'=>$post['meeting_id']];
        $resGuest = db('huiyi_guest')->where($where)->find();
        
        if ($resGuest) {
            $return['guest'] = $resGuest;
            
            // 获取行程
            $where = ['guest_id'=>$resGuest['id'], 'meeting_id'=>$post['meeting_id'], 'status'=>1];
            $resJourney = db('huiyi_journey')->where($where)->find();
            
            if ($resJourney) {
                // 判断状态
                $resJourney['qrcode'] = 'http://' . $_SERVER['HTTP_HOST'] . $resJourney['qrcode']; 
                
                $return['journey'] = $resJourney;
            }
            
            // 获取酒店入住记录
            $where = ['guest_id'=>$resGuest['id'], 'meeting_id'=>$post['meeting_id']];
            $field = 'hotel_id, room_type_name, number, start_time';
            $resHotelLog = db('huiyi_hotel_log')->field($field)->where($where)->find();
            
            if ($resHotelLog) {
                $resHotel = db('huiyi_hotel')->where(['id'=>$resHotelLog['hotel_id']])->find();
                
                $resHotelLog['name'] = $resHotel['name'];
                $resHotelLog['thumb'] = $resHotel['thumb'];
                $resHotelLog['mobile'] = $resHotel['mobile'];
                $resHotelLog['address'] = $resHotel['address'];
                $resHotelLog['long'] = $resHotel['long'];
                $resHotelLog['lat'] = $resHotel['lat'];
                $resHotelLog['distance'] = getMapDistance($resHotel['lat'], $resHotel['long'], $post['lat'], $post['long']);
                $return['hotel'] = $resHotelLog;
            }
            
            // 获取车辆
            $field = 'car_id, driver_mobile, driver_name, status,type,receive_date,receive_time';
            $resCarLog = db('huiyi_car_log')->field($field)->where($where)->find();
    $resCarLog['data'] = date("Y-m-d",$resCarLog['receive_date']).'-'.$resCarLog['receive_time'];
            if ($resCarLog) {
                $resCar = db('huiyi_car')->where(['id'=>$resCarLog['car_id']])->find();

                $resCarLog['number'] = $resCar['number'];
                $resCarLog['name'] = $resCar['name'];
                $resCarLog['color'] = $resCar['color'];
                $resCarLog['long'] = $resMeeting['long'];
                $resCarLog['lat'] = $resMeeting['lat'];
                $resHotelLog['distance'] = getMapDistance($resMeeting['lat'], $resMeeting['long'], $post['lat'], $post['long']);
                $return['car'] = $resCarLog;
            }
        }
        
        suc('success', $return);
    }

    /**
     * @SWG\Post(
     *   path="/modifyJourney",
     *   tags={"Guest"},
     *   summary="修改行程规划",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   
     *   @SWG\Parameter(name="guest_id", description="嘉宾ID", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="name", description="姓名", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="gender", description="性别（男：1，女：2）", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="mobile", description="手机号码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="id_card", description="身份证", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="position", description="职务", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="city", description="城市", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="company", description="单位", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="job_title", description="职称", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="travel_contact", description="行程联系人姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="travel_mobile", description="行程联系人电话", in="formData", required=false, type="string"),
     *
     *   @SWG\Parameter(name="journey_id", description="行程ID", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="vehicle", description="交通工具（飞机，高铁，自理，本地）", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_date", description="来程日期", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_number", description="来程航班", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_start_addr", description="来程航段 - 开始", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_end_addr", description="来程航段 - 结束", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_start_time", description="来程起飞时间", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_end_time", description="来程降落时间", in="formData", required=false, type="string"),
     *
     *   @SWG\Parameter(name="return_date", description="返程日期", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_number", description="返程航班", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_start_addr", description="返程航段  - 开始", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_end_addr", description="返程航段  - 结束", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_start_time", description="返程起飞时间", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="return_end_time", description="返程降落时间", in="formData", required=false, type="string"),
     *
     *   @SWG\Parameter(name="comment", description="注意事项", in="formData", required=false, type="string"),
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据",
     *   )
     * )
     */
    public function modifyJourney()
    {
        $postGuest = request()->only(['meeting_id', 'name', 'gender', 'id_card', 'mobile', 'position', 'city', 'company', 'job_title', 'travel_contact', 'travel_mobile'], 'post');
        
        $postJourney = request()->only(['meeting_id', 'vehicle', 'go_date', 'go_number', 'go_start_addr', 'go_start_time', 'go_start_time', 'go_end_time', 'return_date', 'return_number', 'return_start_addr', 'return_end_addr', 'return_start_time', 'return_end_time', 'comment'], 'post');
        
        // 查询嘉宾信息是否创建
        $whereGuest = ['user_id'=>$this->accessData['uid'], 'meeting_id'=>$postGuest['meeting_id']];
        $resGuest = db('huiyi_guest')->where($whereGuest)->find();
        
        if ( ! $resGuest) {
            err('未增加');
        }
        
        db('huiyi_guest')->where(['id'=>input('post.guest_id')])->update($postGuest);
        
        db('huiyi_journey')->where(['id'=>input('post.journey_id')])->update($postJourney);
        
        suc();
    }

    /**
     * @SWG\Post(
     *   path="/applyUseCar",
     *   tags={"Guest"},
     *   summary="申请用车",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="guest_id", description="嘉宾ID", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="hotel_id", description="酒店ID", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="journey_id", description="行程ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="start_add", description="出发地", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="end_add", description="目的地", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="receive_date", description="用车时间 格式：2018-01-01 12:12:13", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="use_car_time", description="用车时长 - 1：单程，2：半天，3：全天", in="formData", required=true, type="string"),
     *   
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据",
     *   )
     * )
     */
    public function applyUseCar()
    {
        $post = request()->only(['guest_id', 'hotel_id', 'meeting_id', 'journey_id', 'start_add', 'end_add', 'receive_date', 'use_car_time'], 'post');
        
        $post['receive_date'] = strtotime($post['receive_date']);
        $post['receive_time'] = date('H:i', $post['receive_date']);
        $post['type'] = 3;
        $post['status'] = 0;
        $post['create_time'] = $this->timeStamp;
        $res = db('huiyi_car_log')->insert($post);
        
        if ( ! $res) {
            err('增加失败');
        }
        
        suc('增加成功');
    }
    
    /**
     * @SWG\Post(
     *   path="/getEiqList",
     *   tags={"Guest"},
     *   summary="获取问卷调查的题目/结果",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="guest_id", description="嘉宾ID", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据",
     *       @SWG\Header(header="id", type="string", description="题目ID"),
     *       @SWG\Header(header="title", type="string", description="题目标题"),
     *       @SWG\Header(header="type", type="string", description="类型 - 1：选择题(单选)，2：填空题，3：选择题(多选)"),
     *       @SWG\Header(header="answer_a", type="string", description="选项一"),
     *       @SWG\Header(header="answer_b", type="string", description="选项二"),
     *       @SWG\Header(header="answer_c", type="string", description="选项三"),
     *       @SWG\Header(header="answer_d", type="string", description="选项四"),
     *   )
     * )
     */
    public function getEiqList()
    {
        $post = request()->only(['meeting_id', 'guest_id'], 'post');
    
        // 判断是否提交过
        $answer = db('huiyi_eiq_log')->where(['guest_id'=>$post['guest_id'], 'meeting_id'=>$post['meeting_id']])->find();
    
        // 提交过问卷就显示答卷内容
        if ($answer) {
            // 查询问卷题目
            $resTopic = db('huiyi_eiq_log')->alias('log')
                ->join('huiyi_eiq e', 'log.questionnaire_id = e.id')
                ->field('e.id, e.title, e.type, e.answer_a, e.answer_b, e.answer_c, e.answer_d, log.answer')
                ->where(['log.guest_id'=>$post['guest_id'], 'log.meeting_id'=>$post['meeting_id']])
                ->order('sorts desc')
                ->limit(500)
                ->select();
    
            if ( ! $resTopic) {
                err('meeting error');
            }
    
            foreach ($resTopic as $key=>$value) {
                $i = 0;
                $returnData = [];
                $answerArr = json_decode($value['answer'], true);

                // 填空题
                if ($value['type'] == 3) {
                    foreach ($answerArr as $valAnswer) {
                        $returnData[] = $valAnswer;
                    }
    
                    $resTopic[$key]['answers'] = $returnData;
                } else {
                // 选择题
                    if (isset($value['answer_a'])) {
                        $resTopic[$key]['answers'][$i]['answer'] = 'A:' . $value['answer_a'];
                        $resTopic[$key]['answers'][$i]['mark'] = 'A';
    
                        if (array_search('A', $answerArr) !== false) {
                            $resTopic[$key]['answers'][$i]['checked'] = true;
                        } else {
                            $resTopic[$key]['answers'][$i]['checked'] = false;
                        }
    
                        $i ++ ;
                    }
    
                    if (isset($value['answer_b'])) {
                        $resTopic[$key]['answers'][$i]['answer'] = 'B:' . $value['answer_b'];
                        $resTopic[$key]['answers'][$i]['mark'] = 'B';
                        
                        if (array_search('B', $answerArr) !== false) {
                            $resTopic[$key]['answers'][$i]['checked'] = true;
                        } else {
                            $resTopic[$key]['answers'][$i]['checked'] = false;
                        }
    
                        $i ++ ;
                    }
    
                    if (isset($value['answer_c'])) {
                        $resTopic[$key]['answers'][$i]['answer'] = 'C:' . $value['answer_c'];
                        $resTopic[$key]['answers'][$i]['mark'] = 'C';
                        
                        if (array_search('C', $answerArr) !== false) {
                            $resTopic[$key]['answers'][$i]['checked'] = true;
                        } else {
                            $resTopic[$key]['answers'][$i]['checked'] = false;
                        }
    
                        $i ++ ;
                    }
    
                    if (isset($value['answer_d'])) {
                        $resTopic[$key]['answers'][$i]['answer'] = 'D:' . $value['answer_d'];
                        $resTopic[$key]['answers'][$i]['mark'] = 'D';
                        
                        if (array_search('D', $answerArr) !== false) {
                            $resTopic[$key]['answers'][$i]['checked'] = true;
                        } else {
                            $resTopic[$key]['answers'][$i]['checked'] = false;
                        }
                    }
                }
    
                $i = 0;
            }
    
            suc('查询成功', ['type'=>'finish', 'data_answer'=>$resTopic]);
        }
        
        // 如果没有答卷就插入答卷
        if (!$answer){
            // 查询问卷题目
            $resTopic = db('huiyi_eiq')
                ->where(['meeting_id'=>$post['meeting_id']])
                ->field('id, title, type, answer_a, answer_b, answer_c, answer_d')
                ->order('sorts desc')
                ->select();
    
            if ( ! $resTopic) {
                err('meeting error');
            }

            foreach ($resTopic as $key=>$value) {
                $i = 0;
                $returnData = [];
    
                // 填空题
                if ($value['type'] == 3) {
                    for ($i = 0; $i < $value['answer_a']; $i ++ ) {
                        $returnData[] = '';
                    }
    
                    $resTopic[$key]['answers'] = $returnData;
                } else {
                    // 选择题
                    if (isset($value['answer_a'])) {
                        $resTopic[$key]['answers'][$i]['mark'] = 'A';
                        $resTopic[$key]['answers'][$i]['answer'] = 'A:' . $value['answer_a'];
                        $resTopic[$key]['answers'][$i]['checked'] = false;
                        $i ++ ;
                    }
    
                    if (isset($value['answer_b'])) {
                        $resTopic[$key]['answers'][$i]['mark'] = 'B';
                        $resTopic[$key]['answers'][$i]['answer'] = 'B:' . $value['answer_b'];
                        $resTopic[$key]['answers'][$i]['checked'] = false;
                        $i ++ ;
                    }
    
                    if (isset($value['answer_c'])) {
                        $resTopic[$key]['answers'][$i]['mark'] = 'C';
                        $resTopic[$key]['answers'][$i]['answer'] = 'C:' . $value['answer_c'];
                        $resTopic[$key]['answers'][$i]['checked'] = false;
                        $i ++ ;
                    }
    
                    if (isset($value['answer_d'])) {
                        $resTopic[$key]['answers'][$i]['mark'] = 'D';
                        $resTopic[$key]['answers'][$i]['answer'] = 'D:' . $value['answer_d'];
                        $resTopic[$key]['answers'][$i]['checked'] = false;
                    }
                }
    
                $i = 0;
            }
        }
    
        if ($resTopic) {
            suc('查询成功', ['type'=>'unfinished', 'data_answer'=>$resTopic]);
        } else {
            err('查询失败');
        }
    }
    
    /**
     * @SWG\Post(
     *   path="/submitEiq",
     *   tags={"Guest"},
     *   summary="提交问卷",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="guest_id", description="嘉宾ID", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="data", description="提交的答题 - 数组格式", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *   )
     * )
     */
    public function submitEiq()
    {
        $post = request()->only(['meeting_id', 'guest_id', 'journey_id', 'data'], 'post');
        
        $info = db('huiyi_eiq_log')
            ->where(['guest_id'=>$post['guest_id'], 'meeting_id'=>$post['meeting_id']])
            ->find();
        
        if ($info) {
            err('已提交过该问卷');
        }
        
        $insertData = [];
        
        foreach ($post['data'] as $key => $value) {
            $insertData[$key]['guest_id'] = $post['guest_id'];
            $insertData[$key]['meeting_id'] = $post['meeting_id'];
            $insertData[$key]['questionnaire_id'] = $value['id'];
            $insertData[$key]['answer'] = json_encode($value['answer']);
            $insertData[$key]['create_time'] = $this->timeStamp;
        }

        $res = db('huiyi_eiq_log')->insertAll($insertData);
        
        if (!$res) {
            err('提交失败');
        }
        
        // 问卷状态修改
        db('huiyi_journey')->where(['id'=>$post['journey_id']])->update(['is_eiq'=>1]);
        
        suc('提交成功');
    }

    /**
     * @SWG\Post(
     *   path="/userLogin",
     *   tags={"Driver"},
     *   summary="司机端和用户端登陆",
     *   description="注意：请对比用户输入的验证码/手机号与返回的verify_code/mobile，如果其中一项不一致证明验证码失败。",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="mobile", description="手机号", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="type", description="类型 - 用户端：user，司机端：driver", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据如下：",
     *       @SWG\Header(header="mobile", type="string", description="发送短信的手机号"),
     *       @SWG\Header(header="verify_code", type="string", description="验证码"),
     *       @SWG\Header(header="time", type="string", description="短信发送时间"),
     *       @SWG\Header(header="mid", type="string", description="加密字符串"),
     *       @SWG\Header(header="user_id", type="string", description="用户ID"),
     *   )
     * )
     */
    public function userLogin()
    {
        $post = request()->only(['mobile', 'type'], 'post');
        
        $where = [];
        $where['mobile'] = $post['mobile'];
        
        // type - 用户：user，司机：driver
        if ($post['type'] == 'user') {
            $where['type'] = 1;
        } else {
            $where['type'] = 2;
        }
        
        $res = db('huiyi_user')->where($where)->find();
        
        if ( ! $res) {
            err('用户不存在');
        }
        
        $content = '您的验证码是：{code}';
        
        // 发送短信
        $return = $this->publicSendNoteToMobile($post['mobile'], $content,$post['type']);
        
        $return['user_id'] = $res['id'];
        
        suc('success', $return);
    }

    /**
     * @SWG\Post(
     *   path="/getCarListD",
     *   tags={"Driver"},
     *   summary="获取车辆列表",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="user_id", description="用户ID - userLogin.user_id", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据如下：",
     *       @SWG\Header(header="id", type="string", description="车辆ID"),
     *       @SWG\Header(header="name", type="string", description="车辆名称"),
     *       @SWG\Header(header="number", type="string", description="车牌号"),
     *       @SWG\Header(header="color", type="string", description="车的颜色"),
     *   )
     * )
     */
    public function getCarListD()
    {
        $post = request()->only(['user_id'], 'post');
        
        $where = [];
        $where['driver_id'] = $post['user_id'];
        
        $res = db('huiyi_car')->field('id, name, number, color')->where($where)->select();
        
        if ( ! $res) {
            err('无数据');
        }
        
        suc('success', $res);
    }

    /**
     * @SWG\Post(
     *   path="/getWaitOrderListD",
     *   tags={"Driver"},
     *   summary="待完成订单",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="type", description="类型 - 全部订单：all，今日订单：today，明日订单：tomorrow", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     * 
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据如下：",
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="mobile", type="string", description="嘉宾电话"),
     *       @SWG\Header(header="hotel_name", type="string", description="酒店名称"),
     *       @SWG\Header(header="start_add", type="string", description="开始地点 - 机场"),
     *       @SWG\Header(header="end_add", type="string", description="结束地点 - 酒店"),
     *       @SWG\Header(header="receive_time", type="string", description="接机时间"),
     *       @SWG\Header(header="number", type="string", description="航班号"),
     *   )
     * )
     */
    public function getWaitOrderListD()
    {
        $post = request()->only(['page', 'pagesize', 'type'], 'post');
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        $where = ['status'=>1];
        
        // 全部订单
        $allOrder = db('huiyi_car_log')->where($where)->count();
        // 今日订单
        $todayOrder = db('huiyi_car_log')->where($where)->whereTime('receive_date', 'today')->count();
        
        // 明日订单
        $tomoOrder = db('huiyi_car_log')->where($where)->whereTime('receive_date', 'tomorrow')->count();
        
        $where = ['log.status'=>1];
        
        // 酒店、行程、嘉宾、
        if ($post['type'] == 'all') {
            $res = db('huiyi_car_log')->alias('log')
                ->join('huiyi_guest g', 'log.guest_id = g.id', 'left')
                ->join('huiyi_hotel h', 'log.hotel_id = h.id', 'left')
                ->field('log.id, log.type, g.name, g.mobile, log.hotel_name, log.start_add, log.end_add, log.receive_time, log.number, h.long, h.lat')
                ->where($where)
                ->page($post['page'], $post['pagesize'])
                ->select();
        } else {
            if ($post['type'] == 'today') {
                $str = 'today';
            } elseif ($post['type'] == 'tomorrow') {
                $str = 'tomorrow';
            }
            
            $res = db('huiyi_car_log')->alias('log')
                ->join('huiyi_guest g', 'log.guest_id = g.id', 'left')
                ->join('huiyi_hotel h', 'log.hotel_id = h.id', 'left')
                ->field('log.id, log.type, g.name, g.mobile, log.hotel_name, log.start_add, log.end_add, log.receive_time, log.number, h.long, h.lat')
                ->where($where)
                ->whereTime('log.receive_date', $str)
                ->page($post['page'], $post['pagesize'])
                ->select();
        }
        
        $return = [];
        $return['res'] = $res;
        $return['all'] = $allOrder;
        $return['today'] = $todayOrder;
        $return['tomorrow'] = $tomoOrder;
        
        suc('success', $return);
    }

    /**
     * @SWG\Post(
     *   path="/getCompleteOrderListD",
     *   tags={"Driver"},
     *   summary="已完成订单",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="type", description="类型 - 全部订单：all，接机：fetch，送机：send，市内：city", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       description="成功返回数据如下：",
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="mobile", type="string", description="嘉宾电话"),
     *       @SWG\Header(header="hotel_name", type="string", description="酒店名称"),
     *       @SWG\Header(header="start_add", type="string", description="开始地点 - 机场"),
     *       @SWG\Header(header="end_add", type="string", description="结束地点 - 酒店"),
     *       @SWG\Header(header="receive_time", type="string", description="接机时间"),
     *       @SWG\Header(header="number", type="string", description="航班号"),
     *   )
     * )
     */
    public function getCompleteOrderListD()
    {
        $post = request()->only(['page', 'pagesize', 'type'], 'post');
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        // 用车状态 - 0：未安排，1：已安排，5：空驶，6：已完成
        $where = ['log.status'=>['>', 1]];
        
        // 酒店、行程、嘉宾、
        if ($post['type'] == 'all') {
            $res = db('huiyi_car_log')->alias('log')
                ->join('huiyi_guest g', 'log.guest_id = g.id', 'left')
                ->field('log.id, log.type, log.status, log.finish_time, g.name, g.mobile, log.hotel_name, log.start_add, log.end_add, log.receive_time, log.number')
                ->where($where)
                ->page($post['page'], $post['pagesize'])
                ->select();
        } else {
            // 类型 - 1：接机，2送机，3市内
            if ($post['type'] == 'fetch') {
                $where['log.type'] = 1;
            } elseif ($post['type'] == 'send') {
                $where['log.type'] = 2;
            } elseif ($post['type'] == 'city') {
                $where['log.type'] = 3;
            }
            
            $res = db('huiyi_car_log')->alias('log')
                ->join('huiyi_guest g', 'log.guest_id = g.id', 'left')
                ->field('log.id, log.type, log.status, log.finish_time,  g.name, g.mobile, log.hotel_name, log.start_add, log.end_add, log.receive_time, log.number')
                ->where($where)
                ->page($post['page'], $post['pagesize'])
                ->select();
        }
        
        if (!$res) {
            err('无数据');
        }
        
        suc('success', $res);
    }
    
    /**
     * @SWG\Post(
     *   path="/disposeOrder",
     *   tags={"Driver"},
     *   summary="车辆订单处理",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="car_log_id", description="车辆订单ID", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="status", description="状态 - 5：空驶，6：已完成", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="guest", type="string", description="嘉宾信息"),
     *       @SWG\Header(header="journey", type="string", description="行程信息"),
     *       @SWG\Header(header="hotel", type="string", description="酒店信息"),
     *   )
     * )
     */
    public function disposeOrder()
    {
        $post = request()->only(['status', 'car_log_id'], 'post');
        
        if ($post['status'] != 5 and $post['status'] != 6) {
            err('参数错误');
        }
        
        $data = [];
        $data['status'] = $post['status'];
        $data['finish_time'] = $this->timeStamp;
        $res = db('huiyi_car_log')->where(['id'=>$post['car_log_id']])->update($data);
        
        if (!$res) {
            err('提交失败');
        }
        
        suc();
    }
    
    /**
     * @SWG\Post(
     *   path="/locationD",
     *   tags={"Driver"},
     *   summary="定位",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="long", description="纬度", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="lat", description="经度", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="car_id", description="车辆ID", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *   )
     * )
     */
    public function locationD()
    {
        $post = request()->only(['long', 'lat', 'car_id'], 'post');
    
        $info = db('huiyi_car')->where(['id'=>$post['car_id']])->find();
    
        $data = [];
        $data['long'] = $post['long'];
        $data['lat'] = $post['lat'];
        
        $res = db('huiyi_car')->where(['id'=>$post['car_id']])->update($data);
        
        if (!$res) {
            err('添加失败');
        }
        
        suc('添加成功');
    }
    
    /**
     * @SWG\Post(
     *   path="/getRqcodeInfo",
     *   tags={"Staff"},
     *   summary="扫码后获取嘉宾信息",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="param", description="参数 - 格式如：meeting_id=1&guest_id=2", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *   )
     * )
     */
    public function getRqcodeInfo()
    {
        $post = request()->only(['param'], 'post');
        
        $param = parseKeyVal($post['param']);
        
        if (!isset($param['meeting_id']) and !isset($param['guest_id'])) {
            err('参数错误');
        }
        
        $return = [];
        
        // 获取嘉宾信息
        $resGuest = db('huiyi_guest')->field('id, name, company, position, job_title')->where(['id'=>$param['guest_id']])->find();
        $return['guest'] = $resGuest;
        
        // 获取行程
        $where = ['guest_id'=>$resGuest['id'], 'meeting_id'=>$param['meeting_id'], 'status'=>1];
        $field = 'id, return_number, vehicle, return_start_addr, return_end_addr, return_start_time, return_end_time,is_signin';
        $resJourney = db('huiyi_journey')->field($field)->where($where)->find();
        
        if ($resJourney['is_signin'] != 0){
            err('已签到');
        }
        
        if ($resJourney) {
            // 判断状态
            $return['journey'] = $resJourney;
        }
        
        // 获取酒店入住记录
        $where = ['guest_id'=>$resGuest['id'], 'meeting_id'=>$param['meeting_id']];
        $field = 'hotel_name, number';
        $resHotelLog = db('huiyi_hotel_log')->field($field)->where($where)->find();
        
        if ($resHotelLog) {
            $return['hotel'] = $resHotelLog;
        }else{
            $return['hotel'] = ['hotel_name'=>"",'number'=>""];
        }
        
        $where =[];
        $where['create_time'] = date("Y-m-d");
        $where['meeting_id'] = $param['meeting_id'];

        #更新签到次数
        $meeting_log = db('huiyi_meeting_log')->where($where)->field('id,create_time,meeting_id')->find();

        if ($meeting_log){
            db('huiyi_meeting_log')->where(['id'=>$meeting_log['id']])->setInc('signin');
        }else{
            $data = [];
            $data['admin_id'] = $this->accessData['aid'];
            $data['meeting_id'] = $param['meeting_id'];
            $data['signin'] = 1;
            $data['create_time'] = date("Y-m-d");
            db('huiyi_meeting_log')->insert($data);
        }
    
        suc('success', $return);
    }
    
    /**
     * @SWG\Post(
     *   path="/signin",
     *   tags={"Staff"},
     *   summary="签到",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="journey_id", description="行程ID - getRqcodeInfo.journey.id", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *   )
     * )
     */
    public function signin()
    {
        $post = request()->only(['journey_id'], 'post');
        
        $where = ['id'=>$post['journey_id']];
        
        $res = db('huiyi_journey')->where($where)->find();
        
        if (!$res) {
            err('参数错误');
        }
        
        db('huiyi_journey')->where($where)->update(['is_signin'=>$this->timeStamp]);
        
        suc();
    }
    
    /**
     * @SWG\Post(
     *   path="/getMeetingDetailAndStati",
     *   tags={"Staff"},
     *   summary="会议详细、会议统计",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="meeting", type="string", description="会议详情"),
     *       @SWG\Header(header="chart", type="string", description="图表"),
     *       @SWG\Header(header="chart_stati", type="string", description="图表统计"),
     *       @SWG\Header(header="stati", type="string", description="会议统计"),
     *   )
     * )
     */
    public function getMeetingDetailAndStati()
    {
        // 查询会议
        $field = 'id, img, address, start_date, end_date, long, lat';
        $resMeeting = db('huiyi_meeting')->field($field)->where(['status'=>1])->find();
        
        if ( ! $resMeeting) {
            err('暂无会议');
        }
        
        $img = json_decode($resMeeting['img'], true);
        $resMeeting['img'] = $img[0];
        
        // 人数统计 - 图表
        $resChart = db('huiyi_meeting_log')
            ->field('browse, signin, create_time')
            ->where(['meeting_id'=>$resMeeting['id']])
            ->order('create_time asc')
            ->select();
        
        // 统计总数
        $chartTotal = [];
        $chartTotal['browse'] = 0;
        $chartTotal['signin'] = 0;
        
        foreach ($resChart as $value) {
            $chartTotal['browse'] += $value['browse'];
            $chartTotal['signin'] += $value['signin'];
        }
        
        $stati = [];
        
        // 用车
        $whereTotalCar = ['meeting_id'=>$resMeeting['id']];
        $whereFinishCar = ['meeting_id'=>$resMeeting['id'], 'car_log_id'=>['>', 0]];
        
        $stati['carTotalToday'] = db('huiyi_journey')->where($whereTotalCar)->whereTime('go_date', 'today')->count();
        $stati['carTotalTomo'] = db('huiyi_journey')->where($whereTotalCar)->whereTime('go_date', 'tomorrow')->count();
        $stati['carTotalAll'] = db('huiyi_journey')->where($whereTotalCar)->count();
        
        $stati['carFinishToday'] = db('huiyi_journey')->where($whereFinishCar)->whereTime('go_date', 'today')->count();
        $stati['carFinishTotalTomo'] = db('huiyi_journey')->where($whereFinishCar)->whereTime('go_date', 'tomorrow')->count();
        $stati['carFinishTotalAll'] = db('huiyi_journey')->where($whereFinishCar)->count();
        
        // 酒店
        $whereTotalHotel = ['meeting_id'=>$resMeeting['id']];
        $whereFinishHotel = ['meeting_id'=>$resMeeting['id'], 'hotel_id'=>['>', 0]];
        
        $stati['hotelTotalToday'] = db('huiyi_journey')->where($whereTotalHotel)->whereTime('go_date', 'today')->count();
        $stati['hotelTotalTomo'] = db('huiyi_journey')->where($whereTotalHotel)->whereTime('go_date', 'tomorrow')->count();
        $stati['hotelTotalAll'] = db('huiyi_journey')->where($whereTotalHotel)->count();
        
        $stati['hotelFinishToday'] = db('huiyi_journey')->where($whereFinishHotel)->whereTime('go_date', 'today')->count();
        $stati['hotelFinishTotalTomo'] = db('huiyi_journey')->where($whereFinishHotel)->whereTime('go_date', 'tomorrow')->count();
        $stati['hotelFinishTotalAll'] = db('huiyi_journey')->where($whereFinishHotel)->count();
        
        // 嘉宾
        $stati['guestTotal'] = $stati['carTotalAll'];
        $stati['guestFinish'] = db('huiyi_journey')->where(['vehicle'=>['>', 0]])->count();
        
        $returnData = [];
        $returnData['meeting'] = $resMeeting;
        $returnData['chart'] = $resChart;
        $returnData['chart_stati'] = $chartTotal;
        $returnData['stati'] = $stati;
        
        suc('查询成功', $returnData);
    }
    
    /**
     * @SWG\Post(
     *   path="/useCarDemand",
     *   tags={"Staff"},
     *   summary="用车需求",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="type", description="今日：today, 明日：tomorrow，全部：all", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="search", description="搜索 - 嘉宾姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="use", description="用途 - 1：接机，2送机，3市内", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="time", description="用车时长 - 1：单程，2：半天，3：全天", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="is_arrange", description="是否安排 - 9：全部，1：已经安排，0：未安排", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="id", type="string", description="用车ID"),
     *       @SWG\Header(header="guest_id", type="string", description="嘉宾ID"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="receive_date", type="string", description="接机日期"),
     *       @SWG\Header(header="receive_time", type="string", description="接机时间"),
     *       @SWG\Header(header="use_car_time", type="string", description="行程"),
     *       @SWG\Header(header="type", type="string", description="用途"),
     *       @SWG\Header(header="driver_name", type="string", description="司机姓名"),
     *   )
     * )
     */
    public function useCarDemand()
    {
        $post = request()->only(['meeting_id', 'type', 'search', 'use', 'time', 'is_arrange', 'page', 'pagesize']);
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        // 1：接机，2送机，3市内
        $typeArr = [1=>'接机', 2=>'送机', 3=>'市内'];
        
        // 1：单程，2：半天，3：全天
        $timeArr = [1=>'单程', 2=>'半天', 3=>'全天'];
        
        $where = [];
        $where['log.meeting_id'] = $post['meeting_id'];
        
        $whereTwo = '';
        
        // 今日，明日，全部
        if (isset($post['type'])) {
            $today = date('Y-m-d');
            $tomor = date('Y-m-d', strtotime('+1 day'));
            $bermorgen = date('Y-m-d', strtotime('+2 day'));
            
            if ($post['type'] == 'today') {
                $whereTwo['log.receive_date'] = ['between time', [$today, $tomor]];        
            } elseif ($post['type'] == 'tomorrow') {
                $whereTwo['log.receive_date'] = ['between time', [$tomor, $bermorgen]];
            }
        }
        
        if (isset($post['use'])) {
            $where['log.type'] = $post['use'];
        }
        
        if (isset($post['time'])) {
            $where['log.use_car_time'] = $post['time'];
        }
        
        if (isset($post['is_arrange'])) {
            if ($post['is_arrange'] == 1) {
                $where['log.status'] = ['>', 0];
            } elseif ($post['is_arrange'] == 0) {
                $where['log.status'] = 0;
            }
        }
        
        if (isset($post['search'])) {
            $where['g.name'] = ['like', '%' . $post['search'] . '%'];
        }
        
        $whereTime = '';
        
        $res = db('huiyi_car_log')
            ->alias('log')
            ->join('huiyi_guest g', 'log.guest_id = g.id')
            ->field('log.id, g.id as guest_id, g.name, log.receive_date, log.receive_time, log.use_car_time, log.type, log.driver_name')
            ->where($where)
            ->where($whereTwo)
            //->whereTime($whereTime)
            ->page($post['page'], $post['pagesize'])
            ->select();
        
        if (!$res) {
            err('无数据');
        }
            
        foreach ($res as $key => $value) {
            $res[$key]['receive_date'] = date('Y-m-d', $value['receive_date']);
            $res[$key]['type'] = $typeArr[$value['type']];
            $res[$key]['use_car_time'] = $timeArr[$value['use_car_time']];
        }
        
        suc('success', $res);        
    }
    
    /**
     * @SWG\Post(
     *   path="/getHotelDemand",
     *   tags={"Staff"},
     *   summary="酒店需求",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="type", description="今日：today, 明日：tomorrow，全部：all", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="search", description="搜索 - 嘉宾姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="is_arrange", description="是否安排 - 9：全部，1：已安排，0：未安排", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="id", type="string", description="行程ID"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="start_time", type="string", description="入住日期"),
     *       @SWG\Header(header="end_time", type="string", description="离开日期"),
     *       @SWG\Header(header="room_type_name", type="string", description="房型"),
     *       @SWG\Header(header="hotel_name", type="string", description="入住酒店"),
     *       @SWG\Header(header="number", type="string", description="房号"),
     *   )
     * )
     */
    public function getHotelDemand()
    {
        $post = request()->only(['meeting_id', 'type', 'search', 'is_arrange', 'page', 'pagesize']);
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        $where = [];
        $where['j.meeting_id'] = $post['meeting_id'];
        
        // 今日，明日，全部
        if (isset($post['type'])) {
            $today = date('Y-m-d');
            $tomor = date('Y-m-d', strtotime('+1 day'));
            $bermorgen = date('Y-m-d', strtotime('+2 day'));
        
            if ($post['type'] == 'today') {
                $where['j.go_date'] = $today;
            } elseif ($post['type'] == 'tomorrow') {
                $where['j.go_date'] = $tomor;
            }
        }
        
        if (isset($post['is_arrange'])) {
            if ($post['is_arrange'] == 1) {
                $where['j.hotel_id'] = ['>', 0];
            } elseif ($post['is_arrange'] == 0) {
                $where['j.hotel_id'] = 0;
            }
        }
        
        if (isset($post['search'])) {
            $where['g.name'] = ['like', '%' . $post['search'] . '%'];
        }
        
        $res = db('huiyi_journey')
            ->alias('j')
            ->join('huiyi_guest g', 'j.guest_id = g.id','left')
            ->join('huiyi_hotel_log h', 'j.id = h.journey_id','left')
            ->field('j.id, j.go_date, g.id as guest_id, h.hotel_id, g.name, h.start_time, h.end_time, h.room_type_name, h.hotel_name, h.number')
            ->where($where)
            ->page($post['page'], $post['pagesize'])
            ->select();
        
        if (!$res) {
            err('无数据');
        }
        
        suc('success', $res);
    }
    
    /**
     * @SWG\Post(
     *   path="/editHotelDemand",
     *   tags={"Staff"},
     *   summary="编辑住宿需求",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="journey_id", description="行程ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="guest_id", description="嘉宾ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="hotel_id", description="酒店ID", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="hotel_name", description="酒店名称", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="room_type_name", description="房型", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="number", description="房间号", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="start_time", description="入住时间", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="end_time", description="退房时间", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="id", type="string", description="行程ID"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="start_time", type="string", description="入住日期"),
     *       @SWG\Header(header="end_time", type="string", description="离开日期"),
     *       @SWG\Header(header="room_type_name", type="string", description="房型"),
     *       @SWG\Header(header="hotel_name", type="string", description="入住酒店"),
     *       @SWG\Header(header="number", type="string", description="房号"),
     *   )
     * )
     */
    public function editHotelDemand()
    {
        $post = request()->only(['journey_id', 'guest_id', 'meeting_id', 'hotel_id', 'hotel_name', 
            'room_type_name', 'number', 'start_time', 'end_time'], 'post');
        
        $where = [];
        $where['journey_id'] = $post['journey_id'];
        $where['guest_id'] = $post['guest_id'];
        $where['meeting_id'] = $post['meeting_id'];
        
        $res = db('huiyi_hotel_log')->where($where)->find();
        
        if ($res) {
            $hotelID = $res['id'];
            
            $update = [];
            $update['hotel_id'] = $post['hotel_id'];
            $update['hotel_name'] = $post['hotel_name'];
            $update['room_type_name'] = $post['room_type_name'];
            $update['number'] = $post['number'];
            $update['start_time'] = $post['start_time'];
            $update['end_time'] = $post['end_time'];
                                            
            $res = db('huiyi_hotel_log')->where($where)->update($update);
        } else {
            $post['create_time'] = $this->timeStamp;
            
            $res = db('huiyi_hotel_log')->insertGetId($post);
            
            $hotelID = $res;
        }
        
        if (!$res) {
            err();
        }
        
        #更新行程日志酒店数据
        $data = [];
        $data['hotel_id'] = $hotelID;
        $meeting_all = db('huiyi_journey')->where(['id'=>$post['journey_id']])->update($data);
        
        suc();
    }
    
    /**
     * @SWG\Post(
     *   path="/getJourneyDemand",
     *   tags={"Staff"},
     *   summary="机票/火车票需求",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="search", description="搜索 - 嘉宾姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="is_go", description="来程安排 - 9：全部，2：已填写，1：未填写", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="is_return", description="返程安排 - 9：全部，2：已填写，1：未填写", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="id", type="string", description="行程ID"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="mobile", type="string", description="嘉宾手机号"),
     *       @SWG\Header(header="go_number", type="string", description="到达航班"),
     *       @SWG\Header(header="return_number", type="string", description="返程航班"),
     *   )
     * )
     */
    public function getJourneyDemand()
    {
        $post = request()->only(['meeting_id', 'search', 'is_go', 'is_return', 'page', 'pagesize']);
    
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        $where = [];
        $where['j.meeting_id'] = $post['meeting_id'];
    
        if (isset($post['is_go'])) {
            if ($post['is_go'] == 2) {
                $where['j.go_number'] = ['<>', ''];
            } elseif ($post['is_go'] == 1) {
                $where['j.go_number'] = '';
            } 
        }
        
        if (isset($post['is_return'])) {
            if ($post['is_return'] == 2) {
                $where['j.return_number'] = ['<>', ''];
            } elseif ($post['is_return'] == 1) {
                $where['j.return_number'] = '';
            }
        }
        
        if (isset($post['search'])) {
            $where['g.name'] = ['like', '%' . $post['search'] . '%'];
        }
    
        $field = 'j.id, g.name, g.mobile, j.vehicle, j.comment,
            j.go_date, j.go_number, j.go_start_time, j.go_end_time, j.go_start_addr, j.go_end_addr,
            j.return_date, j.return_number, j.return_start_time, j.return_end_time,
            j.return_start_addr, j.return_end_addr';
        
        $res = db('huiyi_journey')
            ->alias('j')
            ->join('huiyi_guest g', 'j.guest_id = g.id')
            ->field($field)
            ->where($where)
            ->page($post['page'], $post['pagesize'])
            ->select();
    
        if (!$res) {
            err('无数据');
        }
            
        suc('success', $res);
    }
    
    /**
     * @SWG\Post(
     *   path="/editJourneyDemand",
     *   tags={"Staff"},
     *   summary="编辑行程",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="id", description="行程ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="go_date", description="来程日期 - 格式：2017-01-01", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_number", description="来程航班号", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_start_time", description="来程出发时间 - 格式：09:30", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_end_time", description="来程到达时间 - 格式：18:20", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="go_start_addr", description="来程出发地", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="go_end_addr", description="来程目的地", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="return_date", description="返程日期 - 格式：2017-01-01", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="return_number", description="返程航班号", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="return_start_time", description="返程出发时间 - 格式：09:30", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="return_end_time", description="返程到达时间 - 格式：18:20", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="return_start_addr", description="返程出发地", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="return_end_addr", description="返程目的地", in="formData", required=true, type="string"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="id", type="string", description="行程ID"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="start_time", type="string", description="入住日期"),
     *       @SWG\Header(header="end_time", type="string", description="离开日期"),
     *       @SWG\Header(header="room_type_name", type="string", description="房型"),
     *       @SWG\Header(header="hotel_name", type="string", description="入住酒店"),
     *       @SWG\Header(header="number", type="string", description="房号"),
     *   )
     * )
     */
    public function editJourneyDemand()
    {
        $post = request()->only(['id', 'go_date', 'go_number', 'go_start_time', 'go_end_time', 'go_start_addr', 'go_end_addr',
            'return_date', 'return_number', 'return_start_time', 'return_end_time', 'return_start_addr', 'return_end_addr', 'comment'], 'post');
    
        $res = db('huiyi_journey')->update($post);
    
        if (!$res) {
            err();
        }
    
        suc();
    }
    
    /**
     * @SWG\Post(
     *   path="/getGuestStatus",
     *   tags={"Staff"},
     *   summary="嘉宾状态",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="search", description="搜索 - 嘉宾姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="id", type="string", description="行程ID"),
     *       @SWG\Header(header="avatarurl", type="string", description="嘉宾头像"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="mobile", type="string", description="嘉宾手机号"),
     *       @SWG\Header(header="is_signin", type="string", description="是否签到 - 0：未签到，大于0：已签到"),
     *       @SWG\Header(header="go_number", type="string", description="到达航班"),
     *       @SWG\Header(header="go_date", type="string", description="到达日期"),
     *       @SWG\Header(header="go_end_time", type="string", description="到达时间"),
     *       @SWG\Header(header="return_number", type="string", description="返程航班"),
     *       @SWG\Header(header="return_date", type="string", description="返程日期"),
     *       @SWG\Header(header="return_start_time", type="string", description="返程时间"),
     *       @SWG\Header(header="hotel_name", type="string", description="酒店名称"),
     *       @SWG\Header(header="number", type="string", description="酒店房间号"),
     *   )
     * )
     */
    public function getGuestStatus()
    {
        $post = request()->only(['meeting_id', 'search', 'page', 'pagesize']);
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        $where = [];
        $where['j.meeting_id'] = $post['meeting_id'];
        
        if (isset($post['search'])) {
            $where['g.name'] = ['like', '%' . $post['search'] . '%'];
        }
        
        $res = db('huiyi_journey')
            ->alias('j')
            ->join('huiyi_guest g', 'j.guest_id = g.id')
            ->join('huiyi_hotel_log l', 'j.hotel_id = l.id', 'left')
            ->field('j.id, g.avatarurl, g.name, g.mobile, j.is_signin, j.go_number, j.go_date, j.go_end_time, j.return_number, j.return_date, j.return_start_time, l.hotel_name, l.number')
            ->where($where)
            ->page($post['page'], $post['pagesize'])
            ->select();
    
        if (!$res) {
            err('无数据');
        }
        
        foreach ($res as $key => $value) {
            $res[$key]['go_date'] = date('m-d', strtotime($value['go_date']));
            $res[$key]['return_date'] = date('m-d', strtotime($value['go_date']));
        }
        
        suc('success', $res);
    }
    
    /**
     * @SWG\Post(
     *   path="/getCarInfo",
     *   tags={"Staff"},
     *   summary="车辆安排 ",
     *   description="",
     *   @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *   @SWG\Parameter(name="meeting_id", description="会议ID", in="formData", required=false, type="integer"),
     *   @SWG\Parameter(name="search", description="搜索 - 嘉宾姓名", in="formData", required=false, type="string"),
     *   @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     *   @SWG\Parameter(name="pagesize", description="每页数据量", in="formData", required=true, type="integer"),
     *
     *   @SWG\Response(
     *       response=200,
     *       @SWG\Header(header="driver_name", type="string", description="司机姓名"),
     *       @SWG\Header(header="driver_mobile", type="string", description="司机手机号"),
     *       @SWG\Header(header="avatarurl", type="string", description="嘉宾头像"),
     *       @SWG\Header(header="name", type="string", description="嘉宾姓名"),
     *       @SWG\Header(header="receive_date", type="string", description="接日期"),
     *       @SWG\Header(header="receive_time", type="string", description="接时间"),
     *       @SWG\Header(header="type", type="string", description="类型"),
     *       @SWG\Header(header="car_name", type="string", description="车辆名称"),
     *       @SWG\Header(header="number", type="string", description="车牌号"),
     *       @SWG\Header(header="color", type="string", description="车颜色"),
     *       @SWG\Header(header="lat", type="string", description="纬度"),
     *       @SWG\Header(header="long", type="string", description="经度"),
     *   )
     * )
     */
    public function getCarInfo()
    {
        $post = request()->only(['meeting_id', 'search', 'page', 'pagesize']);
        
        $result = $this->validate($post, 'Index.page');
        
        if (true !== $result) {
            err($result);
        }
        
        $where = [];
        $where['log.meeting_id'] = $post['meeting_id'];
        
        if (isset($post['search'])) {
            $where['g.name'] = ['like', '%' . $post['search'] . '%'];
        }
        
        $res = db('huiyi_car_log')
            ->alias('log')
            ->join('huiyi_guest g', 'log.guest_id = g.id')
            ->join('huiyi_car c', 'log.car_id = c.id')
            ->field('log.driver_name, log.driver_mobile, g.avatarurl, g.name, log.receive_date, 
                log.receive_time, log.type, c.name as car_name, c.number, c.color, c.lat, c.long')
            ->where($where)
            ->page($post['page'], $post['pagesize'])
            ->select();
        
        if (!$res) {
            err('无数据');
        }
        
        // 1：接机，2送机，3市内
        $typeArr = [1=>'接机', 2=>'送机', 3=>'市内'];
        
        foreach ($res as $key => $value) {
            $res[$key]['receive_date'] = date('Y-m-d', $value['receive_date']);
            $res[$key]['type'] = $typeArr[$value['type']];
        }
        
        suc('success', $res);
    }
    
    public function test()
    {
        $this->createRqcode('meeting_id=2&guest_id=195');
    }
    
    /**
     * 创建二维码
     * @param string $codeContents 二维码上的内容
     * @param number $size
     * @param number $margin
     */
    public function createRqcode($codeContents, $size = 3, $margin = 4)
    {
        Vendor('phpqrcode.phpqrcode');
        $QRcode = new \QRcode();
        
        $tempDir = ROOT_PATH . DS . 'public' . DS;
        $path = '/uploads' . DS . 1004 . DS . 'QRcode' . DS;
        @mkdir($path, 0777, true);

        // generating 
        $fileName = time() . '.jpg';
        
        // frame config values below 4 are not recomended !!! 
        $QRcode::png($codeContents, $tempDir . $path . $fileName, QR_ECLEVEL_L, $size, $margin);
        
        $url = $path . $fileName;
        
        return $url;
    }

    /**
     *统计人数
     */
    public function statistics($meetingId, $type, $create_time)
    {
        $todStart = strtotime(date('Y-m-d 00:00:00', time()));
        $todEnd = strtotime(date('Y-m-d 23:59:59', time()));
        $info = db('meeting_statistics')->where(['create_time'=>['between', [$todStart, $todEnd]], 'meeting_id'=>$meetingId])->find();
        if ($type == 1) {
            if ( ! $info) {
                $data['meeting_id'] = $meetingId;
                $data['create_time'] = $create_time;
                $data['hits'] = 1;
                db('meeting_statistics')->insert($data);
            } else {
                db('meeting_statistics')->where(['id'=>$info['id']])->setInc('hits');
            }
        } elseif ($type == 2) {
            db('meeting_statistics')->where(['id'=>$info['id']])->setInc('enrolments');
        } else {
            db('meeting_statistics')->where(['id'=>$info['id']])->setInc('signs');
        }
    }
    
    //飞机/火车短信
    public function plane_sms()
    {
        $post = request()->only(['name','mobile'],'post');//type = 1,飞机火车短信 type = 2,用车需求

        $verifyCode = mt_rand(1000, 9999);
        

        $content = '您的验证码为' . $verifyCode.'【'.$post['name'].'-您的机票已经安排'.'】';


        if ( ! isset($content)) {
            err('Type param error');
        }
        
        if ($post['mobile'] != '18711112222') {
            $url = 'http://smsapi.c123.cn/OpenPlatform/OpenApi'; //接口地址
            $ac = '1001@500863000001'; //用户账号
            $authkey = '11E42A616C08D53C8E2A2B5D2DE21000'; //认证密钥
            $cgid = '9384'; //通道组编号
            $c = $content; //内容
            $m = $post['mobile']; //号码
            $csid = '9021'; //签名编号 ,可以为空时，使用系统默认的编号
            $t = ''; //发送时间,可以为空表示立即发送,yyyyMMddHHmmss 如:20130721182038
        
        
            $return = $this->sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t);

        } else {
            $verifyCode = 8888;
            $return = ['code'=>1, 'msg'=>'发送成功'];
        }
        
        // 短信发送失败
        if ($return['code'] != 1) {
            err($return['msg']);
        }
        
        $resData = [];
        $resData['mobile'] = $post['mobile'];
        $resData['verify_code'] = $verifyCode;
        $resData['time'] = $this->timeStamp;
        $resData['mid'] = md5($post['mobile'] . $verifyCode . $this->timeStamp . $this->signKey);

        suc('',$resData);

    }
    
    //用车短信
    public function car_sms()
    {
        $post = request()->only(['guest_id','car_type','car_name'],'post');
    
        $verifyCode = mt_rand(1000, 9999);
    
        #获取嘉宾信息
        $guest = db('huiyi_guest')->where(['id'=>$post['guest_id']])->field('name,mobile')->find();


        $content = '您的验证码为' . $verifyCode.'【'.$guest['name'].'-您的'.$post['car_type'].'已经安排'.'-司机'.$post['car_name'].'为您服务'.'】';

    
        if ( ! isset($content)) {
            err('Type param error');
        }
    
        if ($guest['mobile'] != '18711112222') {
            $url = 'http://smsapi.c123.cn/OpenPlatform/OpenApi'; //接口地址
            $ac = '1001@500863000001'; //用户账号
            $authkey = '11E42A616C08D53C8E2A2B5D2DE21000'; //认证密钥
            $cgid = '9384'; //通道组编号
            $c = $content; //内容
            $m = $guest['mobile']; //号码
            $csid = '9021'; //签名编号 ,可以为空时，使用系统默认的编号
            $t = ''; //发送时间,可以为空表示立即发送,yyyyMMddHHmmss 如:20130721182038
    

            $return = $this->sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t);
        } else {
            $verifyCode = 8888;
            $return = ['code'=>1, 'msg'=>'发送成功'];
        }
        
        // 短信发送失败
        if ($return['code'] != 1) {
            err($return['msg']);
        }
        
        $resData = [];
        $resData['mobile'] = $guest['mobile'];
        $resData['verify_code'] = $verifyCode;
        $resData['time'] = $this->timeStamp;
        $resData['mid'] = md5($guest['mobile'] . $verifyCode . $this->timeStamp . $this->signKey);
        
        suc('',$resData);
    }
    
    // 发送短信验证码到手机
    // type - 1：员工，2：司机，3：嘉宾
    public function sendNoteToMobile()
    {
        $post = request()->only(['mobile', 'type'], 'post');
        // 手机号格式错误
        if ( ! isMobile($post['mobile'])) {
            err('mobile error');
        }
        
        $verifyCode = mt_rand(1000, 9999);
        
        if ($post['type'] == '1') {
            $content = '您的验证码为' . $verifyCode;
        } elseif ($post['type'] == 2) {
            $content = '您的验证码为' . $verifyCode;
        } elseif ($post['type'] == 3) {
            $content = '您的验证码为' . $verifyCode;
        }
        
        if ( ! isset($content)) {
            err('Type param error');
        }

        if ($post['mobile'] != '18711112222') {
            $url = 'http://smsapi.c123.cn/OpenPlatform/OpenApi'; //接口地址
            $ac = '1001@500863000001'; //用户账号
            $authkey = '11E42A616C08D53C8E2A2B5D2DE21000'; //认证密钥
            $cgid = '9384'; //通道组编号
            $c = $content; //内容
            $m = $post['mobile']; //号码
            if ($post['type'] == 2){
                $csid = '9021'; //通道组编号
            }else{
                $csid = '9030'; //通道组编号
            }
            $t = ''; //发送时间,可以为空表示立即发送,yyyyMMddHHmmss 如:20130721182038
            
            $return = $this->sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t);
        } else {
            $verifyCode = 8888;
            $return = ['code'=>1, 'msg'=>'发送成功'];
        }
        
        // 短信发送失败
        if ($return['code'] != 1) {
            err($return['msg']);
        }
        
        $resData = [];
        $resData['mobile'] = $post['mobile'];
        $resData['verify_code'] = $verifyCode;
        $resData['time'] = $this->timeStamp;
        $resData['mid'] = md5($post['mobile'] . $verifyCode . $this->timeStamp . $this->signKey);
        
        suc('success', $resData);
    }
    
    // 发送短信验证码到手机
    // type - 1：员工，2：司机，3：嘉宾
    // content = 你的验证码是{code}
    private function publicSendNoteToMobile($mobile, $content,$type=1)
    {
        // 手机号格式错误
        if ( ! isMobile($mobile)) {
            err('mobile error');
        }
        
        $verifyCode = mt_rand(1000, 9999);
        
        $content = str_replace('{code}', $verifyCode, $content);
        
        if ($mobile != '18711112222') {
            $url = 'http://smsapi.c123.cn/OpenPlatform/OpenApi'; //接口地址
            $ac = '1001@500863000001'; //用户账号
            $authkey = '11E42A616C08D53C8E2A2B5D2DE21000'; //认证密钥

            $cgid = '9384'; //通道组编号

            
            $c = $content; //内容
            $m = $mobile; //号码
            if ($type == 2){
                $csid = '9021'; //通道组编号
            }else{
                $csid = '9030'; //通道组编号
            }
            $t = ''; //发送时间,可以为空表示立即发送,yyyyMMddHHmmss 如:20130721182038
            

            $return = $this->sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t);
        } else {
            $verifyCode = 8888;
            $return = ['code'=>1, 'msg'=>'发送成功'];
        }
        
        // 短信发送失败
        if ($return['code'] != 1) {
            err($return['msg']);
        }
        
        $resData = [];
        $resData['mobile'] = $mobile;
        $resData['verify_code'] = $verifyCode;
        $resData['time'] = $this->timeStamp;
        $resData['mid'] = md5($mobile . $verifyCode . $this->timeStamp . $this->signKey);
        
        return $resData;
    }
    
    // 验证短信验证码
    public function verifyNote()
    {
        $post = request()->only(['mobile', 'time', 'mid', 'verify_code']);
        
        if ($post['mid'] != md5($post['mobile'] . $post['verify_code'] . $post['time'] . $this->signKey)) {
            err('验证失败');
        }
        
        suc();
    }

    /*
     * 程序版权：上海创明信息技有限公司 服务热线：4008885262 技术 QQ：2355373292 修改时间：2013-08-18 程序功能：创明网PHP接口示例 通过接口进行单发、群发； 接口说明: http://smsapi.c123.cn/OpenPlatform/OpenApi?action=sendOnce&ac=用户账号&authkey=认证密钥&cgid=通道组编号&c=短信内容&m=发送号码 状态: 1 操作成功 0 帐户格式不正确(正确的格式为:员工编号@企业编号) -1 服务器拒绝(速度过快、限时或绑定IP不对等)如遇速度过快可延时再发 -2 密钥不正确 -3 密钥已锁定 -4 参数不正确(内容和号码不能为空，手机号码数过多，发送时间错误等) -5 无此帐户 -6 帐户已锁定或已过期 -7 帐户未开启接口发送 -8 不可使用该通道组 -9 帐户余额不足 -10 内部错误 -11 扣费失败 短信平台： http://web.c123.cn/main 用户名：50086300 密码：med51669166 接口平台 账号:500863000001 密码:med516691661
     */
    public function sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t)
    {
        $data = array(
                    //发送类型 ，可以有sendOnce短信发送，sendBatch一对一发送，sendParam	动态参数短信接口
                    'action'=>'sendOnce', 
                    //用户账号
                    'ac'=>$ac, 
                    //认证密钥
                    'authkey'=>$authkey, 
                    //通道组编号
                    'cgid'=>$cgid, 
                    //号码,多个号码用逗号隔开
                    'm'=>$m, 
                    //'c'=>iconv('gb2312','utf-8',$c),
                    //如果页面是gbk编码，则转成utf-8编码，如果是页面是utf-8编码，则不需要转码
                    'c'=>$c, 
                    
                    //签名编号 ，可以为空，为空时使用系统默认的签名编号
                    'csid'=>$csid, 
                    //定时发送，为空时表示立即发送
                    't'=>$t);
        
        $xml = $this->postSMS($url, $data); //POST方式提交
        $re = simplexml_load_string(utf8_encode($xml));
        
        //发送成功 ，返回企业编号，员工编号，发送编号，短信条数，单价，余额

        if (trim($re['result']) == 1) {
            foreach ($re->Item as $item) {
                
                $stat['msgid'] = trim((string)$item['msgid']);
                $stat['total'] = trim((string)$item['total']);
                $stat['price'] = trim((string)$item['price']);
                $stat['remain'] = trim((string)$item['remain']);
                $stat_arr[] = $stat;
            }
            if (is_array($stat_arr)) {
                return ['code'=>1, 'msg'=>'发送成功'];
                //echo "发送成功,返回值为" . $re['result'];
            }
        } else {
            $msg = '未知错误';
            //发送失败的返回值
            switch (trim($re['result'])) {
                case 0:
                    $msg = "帐户格式不正确(正确的格式为:员工编号@企业编号)";
                    break;
                case  - 1:
                    $msg = "服务器拒绝(速度过快、限时或绑定IP不对等)如遇速度过快可延时再发";
                    break;
                case  - 2:
                    $msg = " 密钥不正确";
                    break;
                case  - 3:
                    $msg = "密钥已锁定";
                    break;
                case  - 4:
                    $msg = "参数不正确(内容和号码不能为空，手机号码数过多，发送时间错误等)";
                    break;
                case  - 5:
                    $msg = "无此帐户";
                    break;
                case  - 6:
                    $msg = "帐户已锁定或已过期";
                    break;
                case  - 7:
                    $msg = "帐户未开启接口发送";
                    break;
                case  - 8:
                    $msg = "不可使用该通道组";
                    break;
                case  - 9:
                    $msg = "帐户余额不足";
                    break;
                case  - 10:
                    $msg = "内部错误";
                    break;
                case  - 11:
                    $msg = "扣费失败";
                    break;
                default:
                    break;
            }

            
            return ['code'=>trim($re['result']), 'msg'=>$msg];
        }

    }

    public function postSMS($url, $data = '')
    {
        $row = parse_url($url);
        $host = $row['host'];
        $port = 80;
        $file = $row['path'];
        $post = '';
        
        while (list($k, $v) = each($data)) {
            $post .= rawurlencode($k) . "=" . rawurlencode($v) . "&"; //转URL标准码
        }
        $post = substr($post, 0,  - 1);
        $len = strlen($post);
        $fp = @fsockopen($host, $port, $errno, $errstr, 10);
        if ( ! $fp) {
            return "$errstr ($errno)\n";
        } else {
            $receive = '';
            $out = "POST $file HTTP/1.0\r\n";
            $out .= "Host: $host\r\n";
            $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            $out .= "Connection: Close\r\n";
            $out .= "Content-Length: $len\r\n\r\n";
            $out .= $post;
            fwrite($fp, $out);
            while ( ! feof($fp)) {
                $receive .= fgets($fp, 128);
            }
            fclose($fp);
            $receive = explode("\r\n\r\n", $receive);
            unset($receive[0]);
            return implode("", $receive);
        }
    }
}
